﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;
using System.Collections.Generic;
using SilverlightApp.data;
using System.Collections;
using System.Collections.ObjectModel;
using SilverlightApp.Common;
using System.Windows.Threading;

namespace SilverlightApp.Web
{

    public class TemperatureChartBuilder
    {
        public Chart chart = null;
        private MyObservableCollection<PairInfo> DataSource = null;
        private double _WarningMinValue = double.MinValue;
        private double _WarningMaxValue = double.MaxValue;

        public double WarningMinValue { get { return _WarningMinValue; } set { _WarningMinValue = value; } }
        public double WarningMaxValue { get { return _WarningMaxValue; } set { _WarningMaxValue = value; } }

        private DispatcherTimer warningDispatcherTimer = new DispatcherTimer();

        public void BuildChart(Panel panel, MyObservableCollection<PairInfo> ds)
        {
            DataSource = ds;
            chart = new Chart { Title = "温度监控" };
            chart.Height = 230;
            Style legendStyle = new System.Windows.Style();
            Setter legendWidth = new Setter(System.Windows.Controls.Control.WidthProperty, 0);
            Setter legendHeight = new Setter(System.Windows.Controls.Control.HeightProperty, 0);
            legendStyle.Setters.Add(legendWidth);
            legendStyle.Setters.Add(legendHeight);
            legendStyle.TargetType = typeof(System.Windows.Controls.Control);
            chart.LegendStyle = legendStyle;
            DataPointSeries series = new ColumnSeries();

            series.ItemsSource = ds;
            //series.Title = 0 + "°C";
            warningDispatcherTimer.Interval = TimeSpan.FromSeconds(0.3);
            warningDispatcherTimer.Tick += new EventHandler(warningDispatcherTimer_Tick);

            ds.AfterPropertyChanged = (self) =>
            {
                if (self != null && self.Count > 0)
                {
                    //series.Title = Math.Round(self[0].Value, 2) + "°C";
                    chart.Title = string.Format("温度监控" + Environment.NewLine + "({0}°C)", Math.Round(self[0].Value, 2));
                    if (self[0].Value < WarningMinValue || self[0].Value > WarningMaxValue)
                    {
                        StartDispatcherTimer();
                    }
                    else
                    {
                        EndDispatcherTimer();
                    }
                }
            };

            series.IndependentValueBinding = new System.Windows.Data.Binding("Name");
            series.DependentValueBinding = new System.Windows.Data.Binding("Value");
            Style lineStyle = new System.Windows.Style();
            lineStyle.TargetType = typeof(System.Windows.Controls.Control);
            Setter setterRed = new Setter(System.Windows.Controls.Control.BackgroundProperty, new SolidColorBrush(Colors.Red));
            lineStyle.Setters.Add(setterRed);
            series.DataPointStyle = lineStyle;
            series.Width = 100;
            LinearAxis valueAxis = new LinearAxis { Orientation = AxisOrientation.Y, Minimum = 0, Maximum = 100, ShowGridLines = true };
            valueAxis.Interval = 20;
            chart.Axes.Add(valueAxis);
            SeriesTemp = series;
            chart.Series.Add(series);
            Grid grid = new Grid();
            grid.Children.Add(chart);
            panel.Children.Add(grid);
        }
        private DataPointSeries SeriesTemp = null;
        private void StartDispatcherTimer()
        {
            warningDispatcherTimer.Start();
        }

        private void EndDispatcherTimer()
        {
            if (warningDispatcherTimer.IsEnabled)
            {
                warningDispatcherTimer.Stop();
                DataPointSeries series = SeriesTemp;
                Style tempLineStyle = new System.Windows.Style();
                
                tempLineStyle.Setters.Add(new Setter(System.Windows.Controls.Control.BackgroundProperty, new SolidColorBrush(Color.FromArgb(255, 255, 0, 0))));
                isOp = !isOp;
                series.DataPointStyle = tempLineStyle;
            }
        }

        private bool isOp = false;

        void warningDispatcherTimer_Tick(object sender, EventArgs e)
        {
            DataPointSeries series = SeriesTemp;
            Style tempLineStyle = new System.Windows.Style();
            tempLineStyle.TargetType = typeof(System.Windows.Controls.Control);
            Setter setterRed = null;

            if (isOp)
            {
                setterRed = new Setter(System.Windows.Controls.Control.BackgroundProperty, new SolidColorBrush(Color.FromArgb(255, 255, 0, 0)));
            }
            else
            {
                setterRed = new Setter(System.Windows.Controls.Control.BackgroundProperty, new SolidColorBrush(Color.FromArgb(180, 255, 0, 0)));
            }
            
            tempLineStyle.Setters.Add(setterRed);
            isOp = !isOp;
            series.DataPointStyle = tempLineStyle;
        }
    }
}
